<template>
  <el-dialog
    :visible.sync="open"
    :title="title"
    :before-close="cancelSelect"
    width="900px"
    height="500px"
    append-to-body
    :close-on-click-modal="false"
  >
    <el-card>
      <el-form
        :model="queryParams"
        ref="queryForm"
        :inline="true"
        v-show="showSearch"
        label-width="75px"
      >
        <el-form-item label="代码" prop="supplierNo">
          <el-input
            v-model="queryParams.supplierNo"
            placeholder="请输入供应商代码"
            clearable
            size="small"
            @keyup.enter.native="handleQuery"
          />
        </el-form-item>
        <el-form-item label="名称" prop="supplierName">
          <el-input
            v-model="queryParams.supplierName"
            placeholder="请输入供应商名称"
            clearable
            size="small"
            @keyup.enter.native="handleQuery"
          />
        </el-form-item>
        <el-form-item label="快捷码" prop="shortcutCode">
          <el-input
            v-model="queryParams.shortcutCode"
            placeholder="请输入快捷码"
            clearable
            size="small"
            @keyup.enter.native="handleQuery"
          />
        </el-form-item>
        <el-form-item>
          <el-button
            type="cyan"
            icon="el-icon-search"
            size="mini"
            @click="handleQuery"
            >搜索</el-button
          >
          <el-button icon="el-icon-refresh" size="mini" @click="resetQuery"
            >重置</el-button
          >
        </el-form-item>
      </el-form>
      <el-table
        v-loading="loading"
        :data="supplierList"
        @selection-change="handleSelectionChange"
      >
        <el-table-column type="selection" width="55" align="center" />
        <el-table-column label="供应商代码" align="center" prop="supplierNo" />
        <el-table-column
          label="供应商名称"
          align="center"
          prop="supplierName"
        />
        <el-table-column label="快捷码" align="center" prop="shortcutCode" />
        <el-table-column label="供应商地址" align="center" prop="address" />
        <el-table-column label="供应商联系人" align="center" prop="contactor" />
        <el-table-column
          label="供应商移动电话"
          align="center"
          prop="telephoneNumber"
        />
        <el-table-column label="固定电话" align="center" prop="phonenumber" />
        <el-table-column
          label="供应商是否冻结"
          align="center"
          prop="frozenFlag"
          :formatter="frozenFlagFormat"
        />
        <el-table-column
          label="是否有效"
          align="center"
          prop="deleteFlag"
          :formatter="deleteFlagFormat"
        />
        <el-table-column
          label="操作"
          align="center"
          class-name="small-padding fixed-width"
        >
          <template slot-scope="scope">
            <el-button
              size="mini"
              type="text"
              icon="el-icon-edit"
              @click="handleUpdate(scope.row)"
              v-hasPermi="['systembasic:supplier:edit']"
              >修改</el-button
            >
            <el-button
              size="mini"
              type="text"
              icon="el-icon-delete"
              @click="handleDelete(scope.row)"
              v-hasPermi="['systembasic:supplier:remove']"
              >删除</el-button
            >
          </template>
        </el-table-column>
      </el-table>

      <pagination
        v-show="total > 0"
        :total="total"
        :page.sync="queryParams.pageNum"
        :limit.sync="queryParams.pageSize"
        @pagination="getList"
      />
      <el-form label-width="100px">
              <el-form-item
                style="text-align: center; margin-left: -100px; margin-top: 50px"
              >
                <el-button type="primary" @click="confirmSelect"
                  >确 定</el-button
                >
                <el-button @click="cancelSelect">取 消</el-button>
              </el-form-item>
            </el-form>
    </el-card>
  </el-dialog>
</template>

<script>
import {
  listSupplier,
  getSupplier,
  delSupplier,
  addSupplier,
  updateSupplier,
  exportSupplier
} from "@/api/systembasic/supplier";

export default {
  name: "SelectSupplierDialog",
  components: {},
  props: {
    open: {
      type: Boolean,
      default: false
    }
  },
  data() {
    return {
      // 遮罩层
      loading: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 供应商管理表格数据
      supplierList: [],
      // 弹出层标题
      title: "",
      selectedSupplier:[],
      // 供应商是否冻结字典
      frozenFlagOptions: [],
      // 是否有效字典
      deleteFlagOptions: [],
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        supplierNo: null,
        supplierName: null,
        shortcutCode: null,
        address: null,
        contactor: null,
        telephoneNumber: null,
        phonenumber: null,
        frozenFlag: null,
        deleteFlag: null
      }
    };
  },
  created() {
    this.getList();
    this.title="选择供应商";
    this.getDicts("sys_yes_no").then(response => {
      this.frozenFlagOptions = response.data;
    });
    this.getDicts("sys_yes_no").then(response => {
      this.deleteFlagOptions = response.data;
    });
  },
  methods: {
    /** 查询供应商管理列表 */
    getList() {
      this.loading = true;
      listSupplier(this.queryParams).then(response => {
        this.supplierList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    // 供应商是否冻结字典翻译
    frozenFlagFormat(row, column) {
      return this.selectDictLabel(this.frozenFlagOptions, row.frozenFlag);
    },
    // 是否有效字典翻译
    deleteFlagFormat(row, column) {
      return this.selectDictLabel(this.deleteFlagOptions, row.deleteFlag);
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.selectedSupplier = null;
      if (selection.length > 0) {
        this.selectedSupplier = selection[0];
      }
      if (selection.length > 1) {
        this.$refs.mt.clearSelection();
        this.$refs.mt.toggleRowSelection(selection.pop());
      }
    },
    // 选中物料
    confirmSelect() {
      this.$emit("select", this.selectedSupplier);
    },
    // 取消选中
    cancelSelect() {
      this.$emit("cancel");
    }
  }
};
</script>
